package cn.vendsystem.com.controller;


import cn.vendcommon.com.entity.InventoryItem;
import cn.vendcommon.com.entity.Machine;
import cn.vendcommon.com.entity.Result;
import cn.vendsystem.com.service.InventoryService;
import cn.vendsystem.com.service.MachineService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/machine")
public class MachineController {

    @Resource
    private MachineService machineService;
    @Resource
    private InventoryService inventoryService;

    /**
     * 添加售卖机
     * @param machine 售卖机
     * @return 新增的售卖机
     */
    @PostMapping
    public Result<Machine> addMachine(@RequestBody Machine machine) {
        return Result.success(machineService.addMachine(machine));
    }

    /**
     * 删除售卖机
     * @param id 售卖机ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public Result deleteMachine(@PathVariable int id) {
        // 查找售卖机内是否有商品
        List<InventoryItem> inventoryItems = inventoryService.getInventoryByMachineId(id);
        for (InventoryItem item : inventoryItems) {
            if (item.getMachineId() == id) {
                return Result.error("售卖机内有商品，不能删除");
            }
        }
        int result = machineService.deleteMachine(id);
        if(result == 1){
            return Result.success("删除成功");
        }else{
            return Result.error("删除失败");
        }
    }

    /**
     * 更新售卖机
     * @param machine 售卖机
     * @return 更新后的售卖机
     */
    @PutMapping
    public Result<Machine> updateMachine(@RequestBody Machine machine) {
        Machine newMachine = machineService.updateMachine(machine);
        return newMachine != null ? Result.success(newMachine) : Result.error("更新售卖机失败");
    }

    /**
     * 根据售卖机ID查找售卖机
     * @param id 售卖机ID
     * @return 售卖机
     */
    @GetMapping("/{id}")
    public Result<Machine> getMachineById(@PathVariable int id) {
        Machine newMachine = machineService.getMachineById(id);
        return newMachine != null ? Result.success(newMachine) : Result.error("售卖机不存在");
    }

    /**
     * 获取所有售卖机
     * @return 售卖机列表
     */
    @GetMapping
    public Result<List<Machine>> getAllMachines() {
        return Result.success(machineService.getAllMachines());
    }

    /**
     * 根据售卖机ID查找商品
     * @param machineId 售卖机ID
     * @return 商品列表
     */
    @GetMapping("/machine/{machineId}")
    public Result<List<InventoryItem>> getInventoryByMachineId(@PathVariable int machineId) {
        return Result.success(inventoryService.getInventoryByMachineId(machineId));
    }

    /**
     * 根据machineId与intelligentMap扣减库存
     * @param machineId
     * @param intelligentMap
     * @return
     */
    @PostMapping("/deduct/{machineId}")
    public Result deductionInventoryByMachineId(@PathVariable int machineId, @RequestBody Map<String, Integer> intelligentMap) {
        inventoryService.deductionInventoryByMachineId(machineId,intelligentMap);
        return Result.success("扣减成功");
    }
}
